package simplepushserver;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SimpleLogger {

    private File f;
    private ArrayList<LogEntry> history;
    private ArrayList<ChangeListener> listeners;

    public SimpleLogger(String name) {
        this.f = new File(System.getenv("APPDATA"), name);
        listeners = new ArrayList<ChangeListener>();
        readFile();
    }

    private void readFile() {
        if (getLogFile().exists() && getLogFile().canRead()) {
            try {
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(getLogFile()));
                history = (ArrayList<LogEntry>) ois.readObject();
            } catch (Exception ex) {
                history = new ArrayList<LogEntry>();
                JOptionPane.showMessageDialog(null, "Failed to load log");
            }
        } else {
            history = new ArrayList<LogEntry>();
            saveHistory();
            JOptionPane.showMessageDialog(null, "Failed to load log");
        }
    }

    public ArrayList<LogEntry> refreshHistory() {
        readFile();
        return history;
    }

    private boolean saveHistory() {
        try {
            getLogFile().delete();
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(getLogFile()));
            oos.writeObject(history);
            oos.close();
            return true;
        } catch (Exception ex) {
            System.err.println(ex);
            return false;
        }
    }

    public LogEntry getLatest() {
        if (history.isEmpty()) {
            return null;
        }
        return history.get(history.size() - 1);
    }

    private void add(LogEntry s) {
        if (true) {
            history.add(s);
            ChangeEvent ce = new ChangeEvent(s.getSource());
            for (ChangeListener c : listeners) {
                c.stateChanged(ce);
            }
            saveHistory();
        }
    }

    public LogEntry getEntry(int index) {
        return history.get(history.size()-index-1);
    }

    @Override
    protected void finalize() throws Throwable {
        saveHistory();
        super.finalize();
    }

    private File getLogFile() {
        return f;
    }

    private void setLogFile(File f) {
        this.f = f;
        saveHistory();
    }

    public synchronized void log(String source, String action, String content) {
        add(new LogEntry(source, action, content));
    }

    public synchronized void log(String time, String source, String action, String content) {
        add(new LogEntry(time, source, action, content));
    }

    public void addListener(ChangeListener listener) {
        listeners.add(listener);
    }

    public void removeListner(ChangeListener listener) {
        listeners.remove(listener);
    }

    public int size() {
        return history.size();
    }
}